package com.tengju.bff.interfaces.app.message.facade;

import com.google.common.collect.Lists;
import com.tengju.bff.interfaces.app.message.assembler.MessageCentreAssembler;
import com.tengju.bff.interfaces.app.message.vo.MyMessageContentListForm;
import com.tengju.bff.interfaces.app.message.vo.MyMessageContentListVO;
import com.tengju.bff.interfaces.app.message.vo.MyMessageContentVO;
import com.tengju.bff.interfaces.app.message.vo.MyMessageVO;
import com.tengju.support.application.shared.ApplicationException;
import com.tengju.support.application.shared.ApplicationExceptionCode;
import com.tengju.support.domain.model.message.*;
import com.tengju.user.domain.model.user.UserId;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;


@Service
public class MessageCentreFacade {

    @Value("#{${app.message.centre.message.map.type}}")
    private Map<Integer,Integer> messageTypeMap;

    @Autowired
    private MessageRepository messageRepository;


    /***
     * 获取所有未读信息数量
     */
    public Long getAllUnreadMessageCount(UserId userId) {

        List<Integer> typeList = Lists.newArrayList();

        if (messageTypeMap == null || messageTypeMap.isEmpty()) {
            return 0L;
        }
        messageTypeMap.forEach((k,v) -> typeList.add(k));

        List<Integer> messageTypeList = typeList.stream()
                .map(MessageType :: fromVal)
                .filter(Objects::nonNull)
                .map(MessageType :: getVal).collect(Collectors.toList());

        if (CollectionUtils.isEmpty(messageTypeList)) {
            return 0L;
        }

        Long count = messageRepository.getAllUnreadTotal(userId.getValue(),
                messageTypeList,
                ReadStatus.UNREAD.getVal(),
                SendStatus.SEND.getVal());
        return count == null ? Long.valueOf(0) : count;
    }


    /**
     * 我的消息-首页
     */
    public MyMessageVO getMyMessageVOList(UserId userId) {

        List<AppMessageCentreDetail> appMessageCentreDetailList = new ArrayList<>();
        // 消息类型配置
        List<Integer> typeList = Lists.newArrayList();

        if (messageTypeMap == null || messageTypeMap.isEmpty()) {
            return new MyMessageVO();
        }
        messageTypeMap.forEach((k,v) -> typeList.add(k));

        List<Integer> messageTypeList = typeList.stream()
                .map(MessageType :: fromVal)
                .filter(Objects::nonNull)
                .map(MessageType :: getVal).collect(Collectors.toList());

        if (CollectionUtils.isEmpty(messageTypeList)) {
            return new MyMessageVO();
        }

        //获取对应消息类型未读信息条数
        List<AppMessageCentreUnreadCount> unreadCountList = messageRepository.getMessageTypeUnreadTotal(userId.getValue(),
                ReadStatus.UNREAD.getVal(),
                SendStatus.SEND.getVal());

        // 获取对应的消息内容
        for (Integer type : messageTypeList) {
            AppMessageCentreDetail appMessageCentreDetail = messageRepository.getMyMessageTypeDetail(
                    userId.getValue(),
                    type,
                    SendStatus.SEND.getVal()
            );
            appMessageCentreDetailList.add(appMessageCentreDetail);
        }

        return MessageCentreAssembler.toMyMessageVO(unreadCountList,
                appMessageCentreDetailList,
                messageTypeList,
                messageTypeMap);


    }

    /**
     * 消息中心-我的消息-列表页
     */
    public MyMessageContentListVO myMessageContentList(UserId userId,
                                                           MyMessageContentListForm myMessageContentListForm) {

        MyMessageContentListVO myMessageContentListVO = new MyMessageContentListVO();

        myMessageContentListVO.setPageIndex(myMessageContentListForm.getPageIndex());
        myMessageContentListVO.setPageSize(myMessageContentListForm.getPageSize());

        MessageType messageType = MessageType.fromVal(myMessageContentListForm.getMessageType());
        if (messageType == null) {
            throw ApplicationException.error(ApplicationExceptionCode.MESSAGE_TYPE_NOT_EXIST,myMessageContentListForm.getMessageType());
        }

        //查出总条数
        Integer count = messageRepository.getMyMessageListTotal(userId.getValue(),
                messageType.getVal(),
                SendStatus.SEND.getVal());
        myMessageContentListVO.setTotal(count);
        if (myMessageContentListForm.getOffset() >= count) {
            return myMessageContentListVO;
        }

        List<Message> messageContentList = messageRepository.queryMyMessageByPage(userId.getValue(),
                messageType.getVal(),
                SendStatus.SEND.getVal(),
                myMessageContentListForm.getOffset(),
                myMessageContentListForm.getPageSize());

        // 更新未读状态
        messageRepository.updateReadStatus(userId.getValue(),
                messageType.getVal(),
                ReadStatus.UNREAD.getVal(),
                SendStatus.SEND.getVal());

        List<MyMessageContentVO> myMessageContentVOList = MessageCentreAssembler.toMyMessageContentVOList(messageContentList);

        myMessageContentListVO.setMyMessageContentVOList(myMessageContentVOList);

        return myMessageContentListVO;
    }

}
